// myself/apply/BusApplyProgress.js
import notification from '../../utils/notification_center.js';
import api from "../../utils/api.js";
import utils from "../../utils/utils.js";
import network from "../../utils/network.js";
import formatDate from "../../utils/format_date.js";
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    flowbill: null,
    flowlog: null,
    finish: true,
    filename: null,
    filePath: null,
    fileId: null,
    signPath: '',
    nextnameid:'',
    nextname:'',
    mid: 0,
    items: null,
    uid: 0,
    actives: [{
      name: '1',
      value: '同意',
      checked: true
    },
    {
      name: '2',
      value: '不同意',
      checked: false
    },
    ],
    nextShow:false,
    nextShow1:true,
    setQm:0,
    isqm:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      mid: options.mid,
      uid: app.globalData.userInfo.uid
    })
    network.postJson(this, api.gongcheview, {
      adminid: app.globalData.userInfo.uid,
      token: app.globalData.userInfo.token,
      mid: options.mid,
      modelname: options.modelname
    }).then(res => {
      console.log(res.data.data)
      res.data.data.flowbill.purpose = res.data.data.flowbill.purpose.substring(0,10)
      var isshowactive = false
      if(res.data.data.flowbill.nowcheckid){
        var nowcheckid = res.data.data.flowbill.nowcheckid.split(",")
        var index = nowcheckid.indexOf(this.data.uid)
        if(index != -1){
          isshowactive = true
        }
      }
      let showQm = false
      if(res.data.data.isqm == 1 || res.data.data.isqm == 2){
        showQm = true;
      }
      this.setData({
        flowbill: res.data.data.flowbill,
        items: res.data.data.flowlog,
        setQm:res.data.data.isqm,
        nextShow:res.data.data.nextShow,
        isqm:showQm,
        showactive:isshowactive,
        nexuids:res.data.data.nexuids
      })
    })

    this.signature = this.selectComponent("#signature");
    this.toast = this.selectComponent("#toast");
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    notification.removeNotification("GetSignature", this);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  upload() {
    let that = this;
    wx.chooseMessageFile({
      count: 1,
      type: 'all',
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFiles
        let url = app.globalData.debug ? api.debugUrl : api.url
        wx.uploadFile({
          url: url + api.upFile, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0].path,
          name: 'file',
          formData: {
            adminid: app.globalData.userInfo.uid,
            token: app.globalData.userInfo.token,
            cfrom: "weixin",
            device: app.globalData.device
          },
          success(res) {
            const data = res.data
            console.log(JSON.parse(res.data).id)
            that.data.fileId = JSON.parse(res.data).id
            that.setData({
              filename: JSON.parse(res.data).filename
            })
          }
        })
      }
    })
  },
  bindNextChange:function(e){
    let nexuids = e.currentTarget.dataset.nexuids
      wx.navigateTo({
        url: '/myself/file/ChooseUser?nexuids=' + nexuids + '&selectid=' + this.data.nextnameid
    })
    },
  radioChange:function(e){
    let value = e.detail.value
    if(value == 1){
      this.setData({
        nextShow1:true
      })
    }else{
      this.setData({
        nextShow1:false
      })
    }
    let isQm = false;
    if(this.data.setQm == 1){
      isQm = true
    }else{
      if(this.data.setQm == 2 && value == 1){
        isQm = true
      }else if(this.data.setQm == 3 && value == 2){
        isQm = true
      }
    }
    this.setData({
      isqm:isQm
    })
  },

  goSignature() {
    this.signature.openSignature();
  },

  getSignature(src) {
    this.setData({
      path: src.detail
    })
    let that = this;
    let url = app.globalData.debug ? api.debugUrl : api.url
    wx.uploadFile({
      url: url + api.upFile, //仅为示例，非真实的接口地址
      filePath: this.data.path,
      name: 'file',
      formData: {
        adminid: app.globalData.userInfo.uid,
        token: app.globalData.userInfo.token,
        cfrom: "weixin",
        device: app.globalData.device
      },
      success(res) {
        const data = res.data
        that.data.signPath = JSON.parse(res.data).filepath
      }
    })
  },

  /**
 * 请求数据
 * 将每一页的数据放一个数组，即形成一个二维数组,减少页面渲染的次数
 */

  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    if (this.data.isqm && !this.data.signPath) {
      this.toast.showToast('请上传签名');
      return;
    }
    if (e.detail.value.isAgree == 2) {
      if (!e.detail.value.explain) {
        this.toast.showToast('请写明不同意原因');
        return;
      }
    }


    network.postJson(this, api.opt, {
      adminid: app.globalData.userInfo.uid,
      token: app.globalData.userInfo.token,
      zt: e.detail.value.isAgree,
      sm: e.detail.value.explain,
      tuiid: this.data.flowbill.tuiid,
      fileid: '',
      modenum: 'companycar',
      mid: this.data.mid,
      qmimgstr: this.data.signPath,
      nextnameid:this.data.nextnameid,
      nextname:this.data.nextname
    }).then(res => {
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 2000,
        success:function(){
          setTimeout(function () {
            //要延时执行的代码
            wx.navigateBack();
          }, 2000) //延迟时间
        }
      })
    });
  }
})